package cn.yikaojiuguo.student.ui.base;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.cache.disc.naming.HashCodeFileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.utils.StorageUtils;

import java.io.File;
import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.yikaojiuguo.student.R;
import cn.yikaojiuguo.student.adapter.ImgsAdapter;
import cn.yikaojiuguo.student.app.YiKaoApplication;
import cn.yikaojiuguo.student.bean.MessageEvent;
import cn.yikaojiuguo.student.support.simple.AnimationImp;
import cn.yikaojiuguo.student.support.simple.ArgsConstants;
import cn.yikaojiuguo.student.support.utils.LogUtil;
import de.greenrobot.event.EventBus;

public class BaseImgsActivity extends Activity implements ViewPager.OnPageChangeListener, View
        .OnClickListener {

    @Bind(R.id.rl_top)
    RelativeLayout mRl_top;
    @Bind(R.id.iv_back)
    ImageView mIv_back;
    @Bind(R.id.tv_num)
    TextView mTv_num;
    @Bind(R.id.iv_delete)
    ImageView mIv_delete;
    @Bind(R.id.vp)
    public ViewPager mVp;
    protected int mPos = 0;
    protected ArrayList<String> mList;
    protected ImgsAdapter mAdapter;
    private boolean mDelete = false;

    public static Intent newIntent(Class<? extends BaseImgsActivity> clazz,ArrayList<String> pics, int pos, boolean delete) {
        Intent intent = new Intent(YiKaoApplication.getInstance(),clazz);
        intent.putStringArrayListExtra(ArgsConstants.PICS, pics);
        intent.putExtra(ArgsConstants.INDEX, pos);
        intent.putExtra(ArgsConstants.CAN_DELETE, delete);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_imgs);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        initImgLoader();
        initView();
        initEvents();
    }

    private void initEvents() {
        mIv_back.setOnClickListener(this);
        mIv_delete.setOnClickListener(this);
    }

    private void initImgLoader() {
        //初始化ImageLoader
        File cacheDir = StorageUtils.getOwnCacheDirectory(this, ArgsConstants.CACHE_DIR +
                "imageloader/");
        DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder().cacheInMemory
                (true).cacheOnDisk(true).considerExifParams(true).imageScaleType
                (ImageScaleType.EXACTLY).bitmapConfig(Bitmap.Config.RGB_565).displayer(new
                FadeInBitmapDisplayer(100)).resetViewBeforeLoading(true).showImageOnLoading(R
                .drawable.image_default).showImageForEmptyUri(R.drawable.image_default)
                .showImageOnFail(R.drawable.image_default).build();
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
                .denyCacheImageMultipleSizesInMemory().diskCache(new UnlimitedDiskCache(cacheDir))
                .diskCacheFileNameGenerator(new HashCodeFileNameGenerator())
                .defaultDisplayImageOptions(defaultOptions)
                .tasksProcessingOrder(QueueProcessingType.LIFO).build();
        com.nostra13.universalimageloader.core.ImageLoader.getInstance().init(config);
    }

    private void initView() {
        if (getIntent().getBooleanExtra(ArgsConstants.CAN_DELETE, false)) {
            mIv_delete.setVisibility(View.VISIBLE);
        } else {
            mIv_delete.setVisibility(View.GONE);
        }
        mList = getIntent().getStringArrayListExtra(ArgsConstants.PICS);
        mPos = getIntent().getIntExtra(ArgsConstants.INDEX, 0);
        mAdapter = new ImgsAdapter(this, mList);
        mVp.setAdapter(mAdapter);
        mVp.setCurrentItem(mPos);
        mVp.addOnPageChangeListener(this);
        mTv_num.setText((mPos + 1) + "/" + mList.size());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                if(mDelete) {
                    Intent data = new Intent();
                    data.putStringArrayListExtra(ArgsConstants.PICS,mList);
                    setResult(Activity.RESULT_OK,data);
                }
                finish();
                break;
            case R.id.iv_delete:
                deletePic();
                mDelete = true;
                mTv_num.setText((mPos + 1) + "/" + mList.size());
                break;
        }
    }

    protected void deletePic() {
    }

    private void showOrHide() {
        if (mRl_top.getVisibility() == View.VISIBLE) {
            mRl_top.clearAnimation();
            Animation animation = AnimationUtils.loadAnimation(this, R.anim.option_leave_from_top);
            animation.setAnimationListener(new AnimationImp() {
                @Override
                public void onAnimationEnd(Animation animation) {
                    super.onAnimationEnd(animation);
                    mRl_top.setVisibility(View.GONE);
                }
            });
            mRl_top.startAnimation(animation);
        } else {
            mRl_top.setVisibility(View.VISIBLE);
            mRl_top.clearAnimation();
            Animation animation = AnimationUtils.loadAnimation(this, R.anim.option_entry_from_top);
            mRl_top.startAnimation(animation);
        }
    }

    public void onEvent(MessageEvent event){
        if(MessageEvent.MessageType.TOP_SHOW_OR_HIDE == event.messageType) {
            showOrHide();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        LogUtil.showInfoLog("onDestroy", "imgs activity onDestroy");
        android.os.Process.killProcess(android.os.Process.myPid());
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        mPos = position;
        mTv_num.setText((position + 1) + "/" + mList.size());
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onBackPressed() {
        mIv_back.performClick();
    }
}
